﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using SqlMiM.Common.Interfaces;
using SqlMiM.Common.Framework.Configuration;


namespace SqlMiM.SqlIndexes.Options
{
    public class IndexOptionsPage : IOptionsPage
    {

        public const String NAME = "IndexOptionsPage";
        // Default value fot theme
        public const Byte AVG_PCT_FRAG_DEFAULT_VALUE = 15;
        public const String AVG_PCT_FRAG_PROPERTY = "AvgPercentFragmentation";

        public const Boolean GET_SYSTEM_TABLES_DEFAULT_VALUE = false;
        public const String GET_SYSTEM_TABLES_PROPERTY = "GetSystemTables";

        public const Int32 MAX_INDEX_COUNT_DEFAULT_VALUE = 1000;
        public const String MAX_INDEX_COUNT_PROPERTY = "MaxIndexCount";

        // Fields
        private IndexOptions optionControl;
        private ViewModel optionViewModel;

        private ConfigurationObject configurationObject;

        // Methods
        public IndexOptionsPage()
        {
        }

        public void Cancel()
        {
            if (this.optionViewModel != null)
            {
                //this.shellOptionsModel.Cancel();
                this.optionViewModel = null;
                this.optionControl = null;
            }
        }

        public void Commit()
        {
            if (this.optionControl == null)
                return;

            byte newAvgPercentFragmentation = this.optionViewModel.AvgPercentFragmentation;
            byte actualAvgPercentFragmentation = (Byte)this.configurationObject.GetProperty(IndexOptionsPage.AVG_PCT_FRAG_PROPERTY, IndexOptionsPage.AVG_PCT_FRAG_DEFAULT_VALUE);

            if (newAvgPercentFragmentation != actualAvgPercentFragmentation)
                this.configurationObject.SetProperty(IndexOptionsPage.AVG_PCT_FRAG_PROPERTY, newAvgPercentFragmentation);

            Boolean newGetSystemTables = this.optionViewModel.IsSystemDatabasesEnabled;
            Boolean actualGetSystemTables = (Boolean)this.configurationObject.GetProperty(IndexOptionsPage.GET_SYSTEM_TABLES_PROPERTY, IndexOptionsPage.GET_SYSTEM_TABLES_DEFAULT_VALUE);

            if (newGetSystemTables != actualGetSystemTables)
                this.configurationObject.SetProperty(IndexOptionsPage.GET_SYSTEM_TABLES_PROPERTY, newGetSystemTables);

            Int32 newIndexCount = this.optionViewModel.MaxIndexes;
            Int32 actualIndexCount = (Int32)this.configurationObject.GetProperty(IndexOptionsPage.MAX_INDEX_COUNT_PROPERTY, IndexOptionsPage.MAX_INDEX_COUNT_DEFAULT_VALUE);

            if (newIndexCount != actualIndexCount)
                this.configurationObject.SetProperty(IndexOptionsPage.MAX_INDEX_COUNT_PROPERTY, newIndexCount);

            this.optionViewModel = null;
            this.optionControl = null;

        }

        public void Load(ConfigurationObject value)
        {
            this.configurationObject = value;
        }

        // Properties
        public object Content
        {
            get
            {
                if (this.optionControl == null)
                {
                    this.optionControl = new IndexOptions();
                    this.optionViewModel = new ViewModel();

                    byte actualAvgPercentFragmentation = (Byte)this.configurationObject.GetProperty(IndexOptionsPage.AVG_PCT_FRAG_PROPERTY, IndexOptionsPage.AVG_PCT_FRAG_DEFAULT_VALUE);
                    Boolean actualGetSystemTables = (Boolean)this.configurationObject.GetProperty(IndexOptionsPage.GET_SYSTEM_TABLES_PROPERTY, IndexOptionsPage.GET_SYSTEM_TABLES_DEFAULT_VALUE);
                    Int32 actualIndexCount = (Int32)this.configurationObject.GetProperty(IndexOptionsPage.MAX_INDEX_COUNT_PROPERTY, IndexOptionsPage.MAX_INDEX_COUNT_DEFAULT_VALUE);
 
                    this.optionViewModel.AvgPercentFragmentation = actualAvgPercentFragmentation;
                    this.optionViewModel.IsSystemDatabasesEnabled = actualGetSystemTables;
                    this.optionViewModel.MaxIndexes = actualIndexCount;

                    this.optionControl.DataContext = this.optionViewModel;

                }

                return this.optionControl;
            }
        }

        public string Name
        {
            get
            {
                return "Fragmented Indexes";
            }
        }

        public string Title
        {
            get
            {
                return "Fragmented Indexes";
            }
        }
    }

}
